﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using SaleWebsite.library;
using System.Data;

namespace SaleWebsite.Models
{
    public class Employee
    {
        [Required(ErrorMessage = "Name must be not empty"),Range(6,100,ErrorMessage="tên không được  ít hơn 6 kí tự và không được nhiều hơn 100 kí tự")]
        public string Name { get; set; }
        
        [Required(ErrorMessage = "UserName must be not empty"),Range(1,6,ErrorMessage="tên đăng nhập ít nhất là 1 kí tự,nhiều nhất là 6 kí tự")]
        public string UserName { get; set; }
         
        [Required(ErrorMessage = "Password must be not empty")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
         
        [Required(ErrorMessage = "DOB must be not empty")]
        [DataType(DataType.Date)]
        public string DOB { get; set; }
         
        [Required(ErrorMessage = "CMT must be not empty")]
        public string Id_card { get; set; }
         
        [Required(ErrorMessage = "Address must be not empty")]
        public string Address { get; set; }
        
        [Required(ErrorMessage = "Phone must be not empty")]
        [DataType(DataType.PhoneNumber)]
        public string Phone { get; set; }
         
        [Required(ErrorMessage = "Email must be not empty")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }
       
        public ulong Id{get;set;}
        public ulong UserType_Id { get; set; }
        public string UserType_Name { get; set; }


         public ulong Insert()
         {

             try
             {
                 return MySqlImplement.GetInstance().ExcuteQuery(Params.GetAllParams(this), "employee_insert");
             }
             catch (System.Exception ex)
             {
                 throw ex;
             }

         }
         public static DataSet GetAll()
         {
             return MySqlImplement.GetInstance().GetQuery("employee_getall");
         }
         public int Delete()
         {
             return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "employee_delete");
         }
         public int Save()
         {
             try
             {
                 return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "employee_update");
             }
             catch (System.Exception ex)
             {
                 throw ex;
             }
         }
         public DataSet GetGroupById()
         {
             return MySqlImplement.GetInstance().GetQuery("group_get_id", Params.GetAllParams(this));
         }
         private static Employee Convert_one(DataRow row)
         {
             return new Employee()

             {
                 Name = row["name"].ToString(),
                 Id = ulong.Parse(row["id"].ToString()),
                 Email = row["email"].ToString(),
                 Id_card = row["cmt"].ToString(),
                 Phone = row["phone"].ToString(),
                 Address = row["address"].ToString(),
                 DOB=row["dob"].ToString(),
                 UserType_Id=ulong.Parse(row["usertype_id"].ToString()),
                 UserType_Name=row["usertype_name"].ToString()
                 
             };
         }
         public static IEnumerable<Employee> ConvertAll(DataSet data)
         {
             foreach (DataRow row in data.Tables[0].Rows)
             {
                 yield return Convert_one(row);
             }
         }
    }
}